// pages/withdraw/withdraw.js
var httpUtil = require('../../utils/httpUtil.js');
var config = require('../../config.js');
var that;
var user;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tian: '',
    diabled: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    user = wx.getStorageSync("workerInfo");
    that.getMoney();
  },
  
  setMoney: function(e) {
    var money = e.detail.value;
    that.setData({
      tian: money
    });

  },
  tixianquan: function () {
    var that = this;
    that.setData({ 
      tian: that.data.money 
    })
  },

  cashOut: function () {
    that.setData({ disabled: true })
    if (that.data.tian==""){
      wx.showToast({
        title: '请输入提现金额',
        icon: 'false',
        duration: 2000
      })
      that.setData({ disabled: false })
      return false;
    }
    if (that.data.tian==0) {
      wx.showToast({
        title: '金额不能为0',
        icon: 'false',
        duration: 2000
      })
      that.setData({ disabled: false })
      return false;
    }
    httpUtil.req(config.requestUrl+"/cashoutController/workerCashout", {
      "workerCode": user.workerCode,
      "totalSum": that.data.tian
    }, function (res) {
      //res为返回的数据
      if (res.code == 100) {
        wx.showToast({
          title: '提现成功',
          icon: 'success',
          duration: 2000
        });
        setTimeout(function () {
          wx: wx.navigateBack({
            delta: 1,
          })
        }, 2000);
        console.log("提现成功");
        console.log(res);
      } else {
        wx.showModal({
          title: '提现失败',
          content: '服务器返回：' + res.message,
          showCancel: true,
          cancelText: '关闭',
          confirmText: '重试',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击重试')
              that.cashOut();
            } else if (res.cancel) {
              console.log('用户点击关闭')
              wx: wx.navigateBack({
                delta: 1,
              });
            }
          }
        })
        console.log("提现失败");
        console.log(res);
      }
    })
  },
  getMoney: function () {
    httpUtil.req(config.requestUrl +"/workerController/getIndividualMember", {
      "workerId": user.workerId//user.workerCode
    }, function (res) {
      //res为返回的数据
      that.setData({
        money: res.object.noMentionNumber
      });
      console.log(res.object.noMentionNumber);
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})